package com.example.guavaretry.config;

import com.github.rholder.retry.*;
import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * guava重试机制
 */
@Configuration
public class GuavaRetryConfig {

    @Bean
    public Retryer<Boolean> retryer(){
        return RetryerBuilder.<Boolean>newBuilder()
                // 重试条件
                .retryIfException()
                .retryIfRuntimeException()
                .retryIfExceptionOfType(Exception.class)
                .retryIfException(Predicates.equalTo(new Exception()))
                // 如果执行结果为false重试
                .retryIfResult(Predicates.equalTo(false))
                //等待策略：每次请求间隔3min
                .withWaitStrategy(WaitStrategies.fixedWait(3, TimeUnit.MINUTES))
                //停止策略 : 尝试请求10次
                .withStopStrategy(StopStrategies.stopAfterAttempt(10))
                //时间限制 : 某次请求不得超过10s
                .withAttemptTimeLimiter(AttemptTimeLimiters.fixedTimeLimit(10, TimeUnit.SECONDS))
                // 阻塞策略
                .build();
    }
}


